package ubiqweb.spring3.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import ubiqweb.spring3.model.LoginForm;
import ubiqweb.spring3.model.Registration;
import ubiqweb.spring3.model.LoginDetails;
import ubiqweb.spring3.service.LoginService;
import ubiqweb.spring3.service.UserService;

@Controller
@RequestMapping("/registration.html")
public class RegistrationController {
	@Autowired
	private RegistrationValidation registrationValidation;
	
	@Autowired
	  private UserService userService;
	@Autowired
	  private LoginService loginService;

	public void setRegistrationValidation(
			RegistrationValidation registrationValidation) {
		this.registrationValidation = registrationValidation;
	}

	// Display the form on the get request
	@SuppressWarnings("unchecked")
	@RequestMapping(method = RequestMethod.GET)
	public String showRegistration(Map model) {
		Registration registration = new Registration();
		LoginForm loginForm = new LoginForm();
		model.put("registration", registration);
		model.put("loginForm", loginForm);
		
		return "registration";
	}

	// Process the form.
	@RequestMapping(method = RequestMethod.POST)
	public String processRegistration(@Valid Registration registration,
			BindingResult result) {
		// set custom Validation by user
		registrationValidation.validate(registration, result);
		if (result.hasErrors()) {
			return "registration";
		}
		
		registration.setUserType("USR");
		userService.addUser(registration);
		System.out.println("SIZE: "+userService.listUsers().size());
		
		
		System.out.println("NAME: "+userService.listUsers().get(2).getPassword());
		
		return "registrationsuccess";
	}
}
